package com.yehaikeji.workflow.util;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

public class Verification {
    public String createverification(ServletOutputStream sos){
        String scr="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        char csr[]=scr.toCharArray();
        char ver[]=new char[4];
        Random random=new Random();

        BufferedImage bImage=new BufferedImage(100,40,BufferedImage.TYPE_INT_RGB);
        Graphics2D g= bImage.createGraphics();
        g.setColor(Color.white);
        g.fillRect(0,0,100,40);

        for (int i=0;i<ver.length;i++){
            char nowcahr=csr[random.nextInt(csr.length)];
            g.setFont(new Font("Fixedsys", Font.PLAIN,26));
            g.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
            g.drawString(String.valueOf(nowcahr),10+i*22,25);
            ver[i]=nowcahr;
        }

        try {
            ImageIO.write(bImage,"jpeg",sos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return String.valueOf(ver);
    }
}
